<?php

namespace services;

require_once ROOT_SERVICES_MODEL . DS . 'oms.php';
require_once ROOT_SERVICES_MODEL . DS . 'rule.php';
require_once ROOT_SERVICES_MODEL . DS . 'location.php';
require_once ROOT_SERVICES . DS . 'delivery_promises.php';
require_once ROOT_LIBRARY_KLOGGER . DS . 'klogger.php';

use services\models as models;

class service_interface {

    private static $_db;
    private static $_cache;
    private static $_log;
    private static $_init_resource = FALSE;

    public static function set_resource($db, $cache) {
        self::$_cache = $cache;
        self::$_db = $db;
        self::$_log = \KLogger::instance(ROOT_DATA_LOG, \KLogger::DEBUG);
        self::$_init_resource = TRUE;
    }

    public static function get_time_shipping($sku, $location, $date) {
        try {
            if (self::$_init_resource) {
                $oms = new models\oms(self::$_cache);
                $rule_model = new models\rule(self::$_db, self::$_cache);
                $location_model = new models\localtion(self::$_db, self::$_cache);

                $delivery = new delivery_promises(self::$_cache, $oms, $location_model, $rule_model);
                $result = $delivery->get_time($sku, $location, $date);

                return array('resutl' => 'ok', 'data' => $result);
            }
        } catch (\PDOException $pe) {
            self::$_log->logError($pe->getMessage());
            return array('resutl' => 'error', 'data' => '');
        } catch (\Exception $e) {
            self::$_log->logError($e->getMessage());
            return array('resutl' => 'error', 'data' => '');
        }
    }

    public static function get_time_all_location($sku, $date) {
        try {
            if (self::$_init_resource) {
                $oms = new models\oms(self::$_cache);
                $rule_model = new models\rule(self::$_db, self::$_cache);
                $location_model = new models\localtion(self::$_db, self::$_cache);

                $delivery = new delivery_promises(self::$_cache, $oms, $location_model, $rule_model);
                $results = $delivery->get_time_all_location($sku, $date);

                return array('resutl' => 'ok', 'data' => $results);
            }
        } catch (\PDOException $pe) {
            self::$_log->logError($pe->getMessage());
            return array('resutl' => 'error', 'data' => '');
        } catch (\Exception $e) {
            self::$_log->logError($e->getMessage());
            return array('resutl' => 'error', 'data' => '');
        }
    }

}

?>
